// pages/access/access.js
var app = getApp()
var url = app.globalData.url
var orderId = ''
var count1 = 5
var count2 = 5
var count3 = 5
var content = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    names: "评价",
    flag: true,
    tempFilePaths: [],
    length: 0,
    engName:''
  },
  back() {
    wx.navigateBack(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    orderId = options.orderid
    this.getEnName(orderId)
  },
  getpicture: function() {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#CED63A",
      success: function(res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function(type) {
    var that = this;
    wx.chooseImage({
      count: 4 - this.data.tempFilePaths.length,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function(res) {
        console.log(res);

        //上传到自己的服务器
        var filePaths = res.tempFilePaths;
        //多张图片
        for (var index in filePaths) {
          console.log(filePaths[index]);

          wx.uploadFile({
            url: url + '/photo/add_img',
            filePath: filePaths[index],
            name: 'files',
            formData: {
              'user': 'test'
            },
            success: function(res) {

              console.log('123qwe')
              //为空赋值
              for (var i = 0; i < 4; i++) {
                if (that.data.tempFilePaths[i] == null) {
                  console.log("fileq:" + that.data.tempFilePaths[i]);
                  that.data.tempFilePaths[i] = res.data;
                  console.log("fileh:" + that.data.tempFilePaths[i]);
                  break;
                }
              }

              that.setData({

                tempFilePaths: that.data.tempFilePaths,
                length: that.data.tempFilePaths.length
              })
            }


          })

        }

      }
    })
  },
  getEnName() {
    const that = this
    var user = wx.getStorageSync('user')
    wx.request({
      url: url + '/cw_order/engineer_name',
      data: {
        orderId: orderId
      },
      header: {
        'content-type': "application/json"
      },
      method: "get",
      success(res) {
        console.log('师傅')
        console.log(res.data)
        if (res.data.code == 1) {

          that.setData({
            engName: res.data.data.engineerName
          })
        } else {

        }
      }
    })
  },
  toaccess() {
    const that = this
    wx.request({
      url: url + '/comment/insert',
      data: {
        commentOrderId: orderId, //订单id
        commentAverage: count1, //综合评分
        commentAttitude: count2, //服务态度评分
        commentDress: count3, //仪表着装评分
        commentContent: content, //评论内容
        photos: JSON.stringify(that.data.tempFilePaths) // 图片链接拼接用, 拼接
      },
      header: {
        'content-type': "application/json"
      },
      method: "get",
      success(res) {
        console.log("评论")
        console.log(res.data)
        if (res.data.code == 0) {
          wx.showToast({
            title: '评价成功',
            icon: 'success',
            duration: 1000
          })
          app.globalData.currentTab = 2
          wx.navigateBack(1)
        } else {
          wx.showToast({
            title: '服务器繁忙',
            icon: 'loading',
            duration: 1000
          })
        }
      }
    })
  },
  getcontent(e) {
    console.log(e)
    content = e.detail.value
  },
  getcount3(e) {
    console.log(e)
    count3 = e.detail.value
  },
  getcount2(e) {
    console.log(e)
    count2 = e.detail.value
  },
  getcount1(e) {
    console.log(e)
    count1 = e.detail.value
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

})